package com.airnewslinkosky.ticket.ui.lowerlevel;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.OfficeInfo;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.util.DateUtil;
import com.airnewslinkosky.ticket.util.FlyUtil;
import com.mobclick.android.MobclickAgent;
//下级管理详情页
public class LowerLevelDetail  extends BaseActivity implements OnClickListener{
	private Button backBut,searchButton;
	
	private boolean saleExpand = false;
	private LinearLayout saleInfoLay;
	
	private RelativeLayout orderLayout,goxianLayout;
	
	private int mYear;
    private int mMonth;
    private int mDay;
    
	public  final int DATE_B_DIALOG_ID = 11;
	public  final int DATE_E_DIALOG_ID = 12;
	
	private TextView beginDate,endDate;
	private String beginDateStr,endDateStr;
	
	public static OfficeInfo officeInfo;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lower_detail);
		activityList.add(this);
		
		beginDate = (TextView)findViewById(R.id.beginDate);
		endDate = (TextView)findViewById(R.id.endDate);
		//得到当前日期
        final Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
		
		
		beginDate.setOnClickListener(this);
		endDate.setOnClickListener(this);
		
		saleInfoLay = (LinearLayout) findViewById(R.id.sale_info_lay2);
		saleInfoLay.setOnClickListener(this);
		
		backBut = (Button) findViewById(R.id.backBut);
		backBut.setOnClickListener(this);
		
		searchButton = (Button) findViewById(R.id.searchButton);
		searchButton.setOnClickListener(this);
		
		orderLayout = (RelativeLayout) findViewById(R.id.order_layout);
		goxianLayout = (RelativeLayout) findViewById(R.id.goxian_layout);
		
		rendData();
	}

	private void rendData() {
		((TextView)findViewById(R.id.officeId)).setText(officeInfo.OfficeID);
		((TextView)findViewById(R.id.officeName)).setText(officeInfo.OfficeName);
		((TextView)findViewById(R.id.office_status)).setText(FlyUtil.getOfficeStateCodes(Integer.valueOf(officeInfo.OfficeState)));
		((TextView)findViewById(R.id.contactName)).setText(officeInfo.ContactName);
		((TextView)findViewById(R.id.telephone)).setText(officeInfo.Telephone);
		((TextView)findViewById(R.id.mobile)).setText(officeInfo.Mobile);
		((TextView)findViewById(R.id.fax)).setText(officeInfo.Fax);
		((TextView)findViewById(R.id.zip)).setText(officeInfo.ZIP);
		((TextView)findViewById(R.id.email)).setText(officeInfo.Email);
		((TextView)findViewById(R.id.address)).setText(officeInfo.OfficeAddress);
		((TextView)findViewById(R.id.remark)).setText(officeInfo.OfficeDescription);
	}
	
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.backBut:
			Intent intent = new Intent();
			intent.setClass(LowerLevelDetail.this, LowerLevelList.class);
			startActivity(intent);
			break;
		case R.id.searchButton:
			orderLayout.setVisibility(View.VISIBLE);
			goxianLayout.setVisibility(View.VISIBLE);
			break;
		case R.id.sale_info_lay2: 
			if(saleExpand){
				findViewById(R.id.sale_info_lay).setVisibility(View.GONE);
				saleExpand = false;
			} else {
				findViewById(R.id.sale_info_lay).setVisibility(View.VISIBLE);
				saleExpand = true;
			}
			break;
		case R.id.beginDate:
			showDialog(DATE_B_DIALOG_ID);
			break;
		case R.id.endDate:
			showDialog(DATE_E_DIALOG_ID);
			break;
		}
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		    case DATE_B_DIALOG_ID:
		    	return new DatePickerDialog(this, dsl, mYear, mMonth, mDay);
		    case DATE_E_DIALOG_ID:
		    	return new DatePickerDialog(this, ds2, mYear, mMonth, mDay);
		default:
			return null;
		}
	}

    //准备日期设置监听器
    private OnDateSetListener dsl = new DatePickerDialog.OnDateSetListener(){

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear =year;
			mMonth= monthOfYear;
			mDay= dayOfMonth;
			beginDateStr = mYear+"-"+DateUtil.pad(mMonth+1)+"-"+DateUtil.pad(mDay);
			beginDate.setText(beginDateStr);
		}
    };
    
    //准备日期设置监听器
    private OnDateSetListener ds2 = new DatePickerDialog.OnDateSetListener(){

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear =year;
			mMonth= monthOfYear;
			mDay= dayOfMonth;
			endDateStr = mYear+"-"+DateUtil.pad(mMonth+1)+"-"+DateUtil.pad(mDay);
	        endDate.setText(endDateStr);
		}
    };
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
